<?php

namespace EM\NewsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;

class DashboardController extends Controller
{
    
    public function indexAction()     
    {
		$news=$this->getDoctrine()->getRepository('EMNewsBundle:News');
		$categories=$this->getDoctrine()->getRepository('EMNewsBundle:Categories');
		$total = $news->findAll();
		$tcat = $categories->findAll();
		$approved = $news->findBy(array('status'  => 'A'));
		$pending = $news-> findBy(array('status'  => 'P'));
		$declined = $news->findBy(array('status'  => 'D'));
		$drafted =  $news->findBy(array('status'  => 'Dr'));
		
		//$news=$this->getDoctrine()->getRepository('EMNewsBundle:News')->findAll();
			$dt=new \DateTime();
			
			$dt->sub(new \DateInterval('P10D'));
			
			return $this->render('EMNewsBundle:Dashboard:index.html.twig',array('total' => $total,
																				'approved' => count($approved),
																				'pending' => count($pending),
																				'declined' => count($declined),
																				'draft' => count($drafted),
																				'dt' => $dt,
																				'categories' => $tcat
																				));
    }
	
	
	public function dashboardAction()
    {
        return $this->render('EMNewsBundle:Dashboard:index.html.twig');
    }
	
	public function loginAction()
    {
	$request = $this->getRequest();
        $session = $request->getSession();
 
        // get the login error if there is one
        if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(
                SecurityContext::AUTHENTICATION_ERROR
            );
        } else {
            $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
            $session->remove(SecurityContext::AUTHENTICATION_ERROR);
        }
 
        return $this->render(
            'EMNewsBundle:Login:login.html.twig',
            array(
                // last username entered by the user
                'last_username' => $session->get(SecurityContext::LAST_USERNAME),
                'error'         => $error,
            )
        );
    }
	
	
}
